package com.force.jianghuling.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.FrameLayout;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.force.jianghuling.R;
import com.force.jianghuling.base.BaseActivity;
import com.force.jianghuling.entity.TabEntity;
import com.force.jianghuling.fragment.FactionFragment;
import com.force.jianghuling.fragment.HomeFragment;
import com.force.jianghuling.fragment.MyInfoFragment;
import com.force.jianghuling.fragment.ReleaseFragment;
import com.force.jianghuling.fragment.TaskFragment;
import com.force.jianghuling.utils.ViewFindUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

@ContentView(R.layout.activity_my_main)
public class MyMainActivity extends BaseActivity {

    @ViewInject(R.id.tl_tab_change)
    private CommonTabLayout mTabLayout;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles = {"品香阁", "布令堂", "琅琊塔", "聚英殿", "固安居"};
    private int[] mIconUnSelectIds = {R.mipmap.home_off, R.mipmap.task_off, R.mipmap.release_off, R.mipmap.faction_off, R.mipmap.main_off};
    private int[] mIconSelectIds = {R.mipmap.home_on, R.mipmap.task_on, R.mipmap.release_on, R.mipmap.faction_on, R.mipmap.main_on};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>(); //底部实力
    private View mDecorView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFragment();
        initEntities();
        initDataView();
    }

    private void initDataView() {
        mDecorView = getWindow().getDecorView();
        mTabLayout = ViewFindUtils.find(mDecorView, R.id.tl_tab_change);
        mTabLayout.setTabData(mTabEntities, this, R.id.fl_tab_change, mFragments);

        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mTabLayout.setCurrentTab(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        mTabLayout.setCurrentTab(1);

    }

    private void initEntities() {

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnSelectIds[i]));
        }

    }

    private void initFragment() {
        mFragments.add(HomeFragment.newInstance());
        mFragments.add(TaskFragment.newInstance());
        mFragments.add(ReleaseFragment.newInstance());
        mFragments.add(FactionFragment.newInstance());
        mFragments.add(MyInfoFragment.newInstance());
    }

}
